package com.exam.online.system.controller;

import com.exam.online.system.constant.StatusCode;
import com.exam.online.system.entity.CoursePaper;
import com.exam.online.system.entity.Paper;
import com.exam.online.system.model.request.PaperSubmitModel;
import com.exam.online.system.model.request.ReviewPaperModel;
import com.exam.online.system.service.CoursePaperService;
import com.exam.online.system.service.PaperService;
import com.exam.online.system.util.ResponseModel;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.exam.online.system.constant.Constant.SERVICE_PATH_PUBLIC;

/**
 * @author yuzhen
 * @date 2019/5/6
 */
@RestController
@RequestMapping(SERVICE_PATH_PUBLIC)
public class PaperController {

    @Autowired
    private PaperService paperService;
    @Autowired
    private CoursePaperService coursePaperService;

    /**
     * 教师组装试卷
     */
    @PostMapping("/paper/assemblePaper")
    public ResponseModel assemblePaperByTeacher(@Param("teacherId") Integer teacherId,
                                                @Param("questionIds") String questionIds,
                                                @Param("courseName") String courseName,
                                                @Param("name") String name,
                                                @Param("content") String content,
                                                @Param("testTime") Integer testTime) {
        //保存paper
        Paper paper = paperService.savePaper(name, content, courseName, teacherId, testTime, questionIds);
        if (paper == null) {
            return ResponseModel.build(StatusCode.FAILE.getCode(), "保存试卷失败");
        }
        //保存课程和试卷的关系
        CoursePaper coursePaper = coursePaperService.saveCoursePaper(courseName, paper.getId());
        if (coursePaper == null) {
            return ResponseModel.build(StatusCode.FAILE.getCode(), "组装试卷成功，但保存试卷和课程关系失败");
        }
        return ResponseModel.buildFromStateCode(StatusCode.SUCCESS);
    }

    /**
     * 教师发布试卷给学生
     */
    @PostMapping("/paper/issuePaper")
    public ResponseModel issuePaper(@RequestParam("paperId") Integer paperId) {
        return paperService.issuePaper(paperId);
    }

    /**
     * 学生获取试卷详情
     */
    @PostMapping("/paper/detail")
    public ResponseModel paperDetail(@RequestParam("paperId") Integer paperId) {
        return paperService.getPaperDetail(paperId);
    }

    /**
     * 学生提交试卷
     *
     * @param stuId
     * @param paperId
     * @param paperSubmitModel
     * @return
     */
    @PostMapping("/paper/submit")
    public ResponseModel submitPaper(@RequestParam("stuId") Integer stuId,
                                     @RequestParam("paperId") Integer paperId,
                                     @RequestBody PaperSubmitModel paperSubmitModel) {
        List<PaperSubmitModel.QuestionAnswerModel> model = paperSubmitModel.getModel();
        return paperService.calculateScore(stuId, paperId, model);
    }

    /**
     * 教师阅卷(获取试卷答题情况)
     */
    @PostMapping("/paper/checkMark")
    public ResponseModel checkMark(@RequestParam("paperId") Integer paper,
                                   @RequestParam("stuId") Integer stuId) {
        return paperService.checkMarks(paper, stuId);
    }

    /**
     * 教师阅卷的提交评分分数
     *
     * @param reviewPaperModel
     * @return
     */
    @PostMapping("/paper/sumbitScore")
    public ResponseModel sumbitScore(@RequestBody ReviewPaperModel reviewPaperModel) {
        paperService.submitScore(reviewPaperModel);
        return ResponseModel.buildFromStateCode(StatusCode.SUCCESS);
    }

    /**
     * 根据学生id获取未考试的试卷列表
     */
    @PostMapping("/paper/unExamList")
    public ResponseModel listUnExamPaper(@RequestParam("stuId") Integer stuId,
                                         @RequestParam("pageNo") Integer pageNo,
                                         @RequestParam("pageSize") Integer pageSize) {
        return ResponseModel.buildFromStateCode(StatusCode.SUCCESS, paperService.listUnExamPaper(stuId, pageNo, pageSize));
    }

    /**
     * 根据学生id获取已经考试的试卷列表
     */
    @PostMapping("/paper/isExamList")
    public ResponseModel listIsExamPaper(@RequestParam("stuId") Integer stuId,
                                         @RequestParam("pageNo") Integer pageNo,
                                         @RequestParam("pageSize") Integer pageSize) {
        return ResponseModel.buildFromStateCode(StatusCode.SUCCESS, paperService.listIsExamPaper(stuId, pageNo, pageSize));
    }

    /**
     * 教师获取学生考试情况列表
     */
    @PostMapping("/paper/stuScoreList")
    public ResponseModel stuScoreList(@RequestParam("teacherId") Integer teacherId,
                                      @RequestParam("pageNo") Integer pageNo,
                                      @RequestParam("pageSize") Integer pageSize) {

        return ResponseModel.buildFromStateCode(StatusCode.SUCCESS, paperService.stuScoreList(teacherId, pageNo, pageSize));

    }

    /**
     * 通过老师id查找组装的试卷
     */
    @PostMapping("/paper/findPapersByTeacherId")
    public ResponseModel findPaperList(@RequestParam("teacherId") Integer teacherId,
                                       @RequestParam("pageNo") Integer pageNo,
                                       @RequestParam("pageSize") Integer pageSize) {
        return ResponseModel.buildFromStateCode(StatusCode.SUCCESS, paperService.findPaperByTeacherId(teacherId, pageNo, pageSize));
    }
}
